<template>
	<view class="wdh-100 columnbox content">
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">优惠券名称</view>
			<view class="info-detail">
				<input v-model="name" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">描述</view>
			<view class="info-detail">
				<input v-model="title" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">满多少使用</view>
			<view class="info-detail">
				<input type="digit" v-model="fullprice" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">减多少元</view>
			<view class="info-detail">
				<input type="digit" v-model="price" placeholder="请输入" />
			</view>
		</view>
		<!-- <view class="rowbox wdh-100 spb one-info">
			<view class="info-title">截止日期</view>
			<picker @change="chooseDate" mode="date" :start="start">
				<view class="rowbox info-detail">
					<text>{{endtime||'点击选择'}}</text>
					<u-icon name="arrow-right" size="28rpx"></u-icon>
				</view>
			</picker>
		</view> -->
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">每人限领取：（0不限制）</view>
			<view class="info-detail">
				<input v-model="receivenum" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100 spb one-info">
			<view class="info-title">领取后几天过期</view>
			<view class="info-detail">
				<input type="digit" v-model="day" placeholder="请输入" />
			</view>
		</view>
		<view class="rowbox wdh-100">
			<view class="rowbox send-btn grey-btn" v-if="submitflag">确定</view>
			<view class="rowbox send-btn def-bgcol" v-else @click="submitInfo">确定</view>
		</view>

		<u-popup round="8" :show="showShare" @close="showPhonePop=false" mode="center">
			<view class="phonePop">
				<view class="tit">
					提示
				</view>
				<view class="name">
					保存成功，是否分享？
				</view>
				<view class="footer-btn ">
					<button class="btn confirmbtn" @click="handleShare">
						分享
					</button>
					<view class="btn" @click="handleBack">
						取消
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showShare: false,
				day: '',
				name: "",
				title: "",
				endtime: '',
				fullprice: '',
				price: '',
				receivenum: '',
				start: '',
				submitflag: false,
				id: 0,
				updateflag: false
			};
		},
		onShow() {
			let y = new Date().getFullYear()
			let m = new Date().getMonth() + 1
			let d = new Date().getDate()
			this.start = y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d)
		},
		onLoad(options) {
			if (options.id) {
				uni.setNavigationBarTitle({
					title: '编辑优惠券'
				})
				this.id = parseInt(options.id)
				this.updateflag = true
				this.getDetail()
			}
		},
		methods: {
			handleShare() {
				this.showShare = false
				uni.navigateToMiniProgram({
					appId: 'wx42f52aeb0203685a',
					path: `/pages/couponCenter/couponCenter?shanghuid=${uni.getStorageSync('userInfo').shanghuid}`
				})
			},
			handleBack() {
				this.showShare = false
				let pages = getCurrentPages(); //获取所有页面栈实例列表
				let nowPage = pages[pages.length - 1]; //当前页页面实例
				let prevPage = pages[pages.length - 2]; //上一页页面实例
				prevPage.$vm.refreshflag = true
				uni.navigateBack()
			},
			getDetail() {
				this.$req.post('/shanghu/couponinfo', {
					id: this.id
				}).then(res => {
					let info = res.data.data
					this.name = info.name
					this.fullprice = info.fullprice
					this.price = info.price
					this.receivenum = info.receivenum
					this.title = info.title
				})
			},
			chooseDate(e) {
				this.endtime = e.detail.value
			},
			submitInfo() {
				if (this.name == '') {
					uni.showToast({
						title: '请输入优惠券名称',
						icon: 'none'
					})
				} else if (this.title == '') {
					uni.showToast({
						title: '请输入描述',
						icon: 'none'
					})
				} else if (this.fullprice == '') {
					uni.showToast({
						title: '请输入满多少使用',
						icon: 'none'
					})
				} else if (this.price == '') {
					uni.showToast({
						title: '请输入减多少元',
						icon: 'none'
					})
				} else if (this.receivenum == '') {
					uni.showToast({
						title: '请输入每人限领取',
						icon: 'none'
					})
				} else if (this.day == '') {
					uni.showToast({
						title: '请输入领取后几天过期',
						icon: 'none'
					})
				} else {
					if (!this.submitflag) {
						this.submitflag = true
						uni.showLoading({
							title: '正在保存'
						})
						if (this.updateflag) {
							this.$req.post('/shanghu/couponupdate', {
								"id": this.id,
								"shanghuid": uni.getStorageSync('userInfo').shanghuid,
								"name": this.name,
								"title": this.title,
								"fullprice": parseFloat(this.fullprice),
								"price": parseFloat(this.price),
								"receivenum": parseInt(this.receivenum),
								"day": parseInt(this.day)
							}).then(res => {
								uni.hideLoading()
								this.submitflag = false
								if (res.data.code == 0) {
									this.showShare = true
								} else {
									uni.showModal({
										title: '添加失败',
										content: res.data.msg
									})
								}
							})
						} else {
							this.$req.post('/shanghu/couponadd', {
								"shanghuid": uni.getStorageSync('userInfo').shanghuid,
								"name": this.name,
								"title": this.title,
								"fullprice": parseFloat(this.fullprice),
								"price": parseFloat(this.price),
								"receivenum": parseInt(this.receivenum),
								"day": parseInt(this.day)
							}).then(res => {
								uni.hideLoading()
								this.submitflag = false
								if (res.data.code == 0) {
									this.showShare = true
								} else {
									uni.showModal({
										title: '添加失败',
										content: res.data.msg
									})
								}
							})
						}

					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.phonePop {
		width: 80vw;
		display: flex;
		flex-direction: column;
		// align-items: center4
		padding: 30rpx 24rpx 30rpx 24rpx;
		min-height: 400rpx;



		.tit {
			font-size: 30rpx;
			font-weight: 500;
			flex-shrink: 0;
			text-align: center;
		}

		.name {
			font-size: 28rpx;
			margin-top: 30rpx;
			flex: 1;
			padding: 0 20rpx;
			text-align: center;
		}

		.namecenter {
			text-align: center;
		}

		button {
			margin: 0;
			padding: 0;
			line-height: 35rpx;
			background-color: transparent;
		}

		button::after {
			border: none;
		}

		.footer-btn {
			flex-shrink: 0;
			padding-top: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			width: 100%;

			.btn {
				line-height: 35rpx;
				width: calc(50% - 15rpx);
				padding: 20rpx;
				border-radius: 8rpx;
				font-size: 30rpx;
				font-weight: 500;
				text-align: center;
				background-color: #f5f5f5;
			}

			.confirmbtn {
				background-color: #57bd6a;
				color: #fff;
			}
		}

		.btncenter {
			justify-content: center;
		}
	}

	.content {
		padding: 20rpx 20rpx;
	}

	.img-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		align-items: center;
		border-bottom: #f5f5f5 solid 1px;
		padding-bottom: 32rpx;
	}

	.add-box,
	.img-box {
		background: #f5f5f5;
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
		margin-right: 32rpx;
		margin-top: 20rpx;
	}

	.img-box {
		position: relative;

		image {
			width: 150rpx;
			height: 150rpx;
			border-radius: 10rpx;
		}
	}

	.delet-box {
		position: absolute;
		right: -10rpx;
		background: rgba(#000, 0.7);
		width: 40rpx;
		height: 40rpx;
		border-radius: 40rpx;
		top: -10rpx;
	}

	.text-box {
		font-size: 28rpx;
		margin-top: 32rpx;
	}

	.one-info {
		padding: 32rpx 0;
		border-bottom: #f5f5f5 solid 1px;
		font-size: 28rpx;
	}

	.info-title {
		color: #5a5a5a;
	}

	.info-detail {
		text {
			margin-right: 10rpx;
		}

		input {
			font-size: 28rpx;
			text-align: right;
		}
	}

	.send-btn {
		margin-top: 100rpx;
		color: #fff;
		width: 600rpx;
		height: 100rpx;
		border-radius: 20rpx;
	}

	.market-title {
		text-align: center;
		padding: 32rpx 0;
		font-size: 32rpx;
	}

	.market-list {
		height: 50vh;
	}

	.one-market {
		padding: 32rpx 40rpx;
		font-size: 28rpx;
	}

	.market-name {
		width: 500rpx;
	}

	.sel-icon {
		width: 40rpx;
		height: 40rpx;
	}

	.confirm-btn {
		font-size: 28rpx;
		width: 500rpx;
		height: 90rpx;
		color: #fff;
		border-radius: 10rpx;
	}

	.market-choose {
		width: 450rpx;
		margin-right: 10rpx;
		text-align: right;
		// view {
		// 	margin-top: 10rpx;
		// 	margin-right: 10rpx;
		// }
	}

	.add-img {
		background: #f2f0f3;
		width: 150rpx;
		height: 150rpx;
		border-radius: 10rpx;
	}
</style>